//
//  NSObject+CYHTTP.m
//  MyTools
//
//  Created by baidu on 6/8/16.
//  Copyright © 2016 caoyuan. All rights reserved.
//

#import "NSObject+CYHTTP.h"
#import "AFNetworking.h"
#import "CYJSON.h"
@implementation NSObject (CYHTTP)

- (void)get:(NSString *)url
  withParam:(NSDictionary *)paramDict
resultBlock:(void (^)(NSDictionary *responseDict, NSError *error))block {
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    [session GET:url  parameters:paramDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        block(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        block(nil, error);
    }];
}



- (void)post:(NSString *)url
   withParam:(NSDictionary *)paramDict
 resultBlock:(void (^)(NSDictionary *responseDict, NSError *error))block {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    [session POST:url  parameters:paramDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        block(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        block(nil, error);
    }];
    
}


- (NSMutableDictionary*)post:(NSString*)url postDic:(NSMutableDictionary*)postData
{
    NSURL* URL                   = [NSURL URLWithString:url];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSData * jsonDataFromDic     = [CYJSON dicToJSON:postData];
    [request  setHTTPBody:jsonDataFromDic];
    NSData *receiveNSData        = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSMutableDictionary *dic     = [CYJSON JSONtoDic:receiveNSData];
    return dic;
    
}

- (NSMutableDictionary*)get:(NSString*)url
{
    NSURL* URL                   = [NSURL URLWithString:url];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    [request setHTTPMethod:@"GET"];
    NSData *receiveNSData        = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSMutableDictionary *dic     = [CYJSON JSONtoDic:receiveNSData];
    return dic;
    
}

- (NSMutableDictionary*)put:(NSString*)url postDic:(NSMutableDictionary*)postData
{
    NSURL* URL                   = [NSURL URLWithString:url];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    [request setHTTPMethod:@"PUT"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSData * jsonDataFromDic     = [CYJSON dicToJSON:postData];
    [request  setHTTPBody:jsonDataFromDic];
    NSData *receiveNSData        = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSMutableDictionary *dic     = [CYJSON JSONtoDic:receiveNSData];
    NSLog(@"%@",dic);
    return dic;
    
}


@end
